package dtaskboard.server.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

public abstract class GenericDao<T> {


  abstract public Class<T> getEntityClass();


  public T find(Long id) {
    return EMFactory.get().find(this.getEntityClass(), id);
  }

  @SuppressWarnings("unchecked")
  public List<T> list() {
    Query q =
        EMFactory.get().createQuery("FROM " + this.getEntityClass().getSimpleName());
    return q.getResultList();
  }

  public void save(T t) {
    EntityManager em = EMFactory.get();
    em.getTransaction().begin();
    em.persist(t);
    em.getTransaction().commit();
  }

  public void update(T t) {
    EntityManager em = EMFactory.get();
    em.getTransaction().begin();
    em.merge(t);
    em.getTransaction().commit();
  }

  public void remove(T t) {
    EntityManager em = EMFactory.get();
    em.getTransaction().begin();
    em.remove(t);
    em.getTransaction().commit();
  }


}
